# CombinedImages.py # # Description: Take two images (2 files) as input # 1) Image A is the foreground object in front of a green screen # 2) Image B is the background # and combine them. # Once combined, the resulting image has the foreground object in # front of the background. # # Author: AL and AL # Date: 2024 # Import necessary Python Imaging Library (PIL) from PIL import Image # Import our own colour module!!! import myColourModule # ***Main part of our program # Create output image - result of combining images imageOutput = Image.open("kid-green.jpg") # Get width and height of image width = imageOutput.width height = imageOutput.height # Open the file containing the foreground object with green screen -> A # and load its content imageKidOpen = Image.open("kid-green.jpg") imageKidLoaded = imageKidOpen.load() # Open the file containing the background -> B # and load its content imageBeachOpen = Image.open("beach.jpg") imageBeachLoaded = imageBeachOpen.load() # Go through each pixel of A # First: for each row for j in range(height): # Second: for each cell in each row for i in range(width): r = imageKidLoaded[i,j][0] # Red part of the RGB colour value (tuple) g = imageKidLoaded[i,j][1] # Green part of the RGB colour value (tuple) b = imageKidLoaded[i,j][2] # Blue part of the RGB colour value (tuple) # If it is green if myColourModule.isPixelGreen(r,g,b): # We replace it with the corresponding pixel in B # Get the coordinates of this pixel xy = (i,j) # Get the colour tupple (r,g,b) of the corresponding pixel in B # -> image of the beach colour = imageBeachLoaded[i,j] # Change the colour of the corresponding pixel in the output image # to the colour of the corresponding pixel in the beach image # Note: The putpixel( ) function takes two arguments. # The first one is the coordinates of the pixel in the image that we # want to modify, and the second argument is the colour to modify it to. imageOutput.putpixel(xy, colour) # Save the resulting image imageOutput.save("combinedImages.png", "png") # Close the image files imageKidOpen.close() imageBeachOpen.close() imageOutput.close()